package pl.uz.zgora.plan;

import java.io.IOException;
import org.ksoap2.SoapFault;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoUpdateWebserviceReciever extends BroadcastReceiver {

	/** Treść powiadomienia */
	private static CharSequence contentText = null;
	/** Tytuł powiadomienia */
	private static CharSequence contentTitle = null;
	/** Tekst wyświetlany na górnej belce */
	private static CharSequence tickerText = null;
	/** Obiekt opisujący powiadomienie pojawiające się na górnej belce */
	private static Notification mNotification = null;
	/** Systemowy manager powiadomień */
	private static NotificationManager mNotificationManager = null;
	/** Identyfikator powiadomienia */
	final private static int NOTIFICATION_ID = 1;
	@Override
	public void onReceive(Context context, Intent intent) {
		
			tickerText = "Plan UZ";
			contentTitle = "Automatyczna aktualizacja";
			contentText = "Rozpoczynanie aktualizacji";
			mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
			
			final Intent mainMenuIntent = new Intent(context, MainMenuActivity.class);
			
			int icon = R.drawable.ic_launcher;
			
			long when = System.currentTimeMillis();

			mNotification = new Notification(icon, tickerText, when);

			final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainMenuIntent, 0);

			mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
						
			mNotificationManager.notify(NOTIFICATION_ID, mNotification);

			if(HelperMethodsManager.isOnline(context)) {
				PlanUzDataSource planDataSource = new PlanUzDataSource(
						context);
				Plan plan = planDataSource.readPlan();
				final PrefrencesManager pm = PrefrencesManager.getInstance(context);
				final String appMode = pm.getStringFromPreferences("appmode");
				pm.addBooleanToPreferences("isUpdating", true);
				final SOAPManager soapManager = new SOAPManager(context);
				try {
					if (appMode.equals("teacher")) {
						// Tryb nauczyciel
						final Nauczyciel nauczyciel = plan.getNauczycieli().get(0);
						plan = soapManager.downloadTeacherTimeTable(nauczyciel);
					} else if(appMode.equals("student")) {
						// Tryb student
						final Grupa grupa = plan.getGroup();
						plan = soapManager.downloadGroupTimeTable(grupa);
					}
					if(!plan.getPrzedmioty().isEmpty()) {
						planDataSource = new PlanUzDataSource(context, true);
						if(!planDataSource.writePlan(plan)) {
							contentText = "Błąd podczas zapisu planu";
							mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
							mNotificationManager.notify(NOTIFICATION_ID, mNotification);
							pm.addBooleanToPreferences("isUpdating", false);
						} else {
							contentText = "Zakończona sukcesem";
							mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
							mNotificationManager.notify(NOTIFICATION_ID, mNotification);
							pm.addBooleanToPreferences("isUpdating", false);
						}
					}
				}catch (SoapFault e) {
					pm.addBooleanToPreferences("isUpdating", false);
					showErrorNotification(contentIntent, context, e.getLocalizedMessage());
				} catch (IOException e) {
					pm.addBooleanToPreferences("isUpdating", false);
					showErrorNotification(contentIntent, context, e.getLocalizedMessage());
				} catch (XmlPullParserException e) {
					pm.addBooleanToPreferences("isUpdating", false);
					showErrorNotification(contentIntent, context, e.getLocalizedMessage());
				}
			} else {
				showErrorNotification(contentIntent, context, "Brak połączenia z internetem");
			}
	}
	
	private void showErrorNotification(PendingIntent contentIntent,
			Context context, final String msg) {
		contentText = msg;
		mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		mNotificationManager.notify(NOTIFICATION_ID, mNotification);
	}
}
